package com.dao;

import java.sql.*;


public class MovesJdbc {
	  Connection conn;
	  Statement stem;
	  ResultSet rs ;
	  public  void  lianjie (){
		  try {
			Class.forName("com.mysql.jdbc.Driver");
			//数据库用户名密码要写自己的
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/abc", "root", "5258");
		} catch (Exception e) {
			e.printStackTrace();
		} 
	  }
	
	  public ResultSet show(){
          this.lianjie();
          // 向数据库发送sql 语句的
		try {
			  stem = conn.createStatement();
			  rs = stem.executeQuery(" select  * from  moves");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
          return  rs;
	  }
	 
	public  void  close(){
		  try {
			if(conn!=null){
				  conn.close();
			  }

			  if(stem!=null){
				  stem.close();
			  }

			  if(rs!=null){
				  rs.close();
			  }
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void add(String file, String author, String time,String hits,String statues) {
		this.lianjie();
		try {
			stem=conn.createStatement();
			stem.executeUpdate("insert into moves values(0, '"+file+"', '"+author+"' , '"+time+"','"+hits+"','"+statues+"')");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.close();
	}

	public void delete(String id) {
		this.lianjie();
		try {
			stem=conn.createStatement();
			stem.executeUpdate("delete from moves where mid='"+id+"' ");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		this.close();
	}

	public void update(String mid,String file, String author, String time,String hits,String statues) {
		this.lianjie();
		try {
			stem=conn.createStatement();
			stem.executeUpdate(" update moves set file='"+file+"',author='"+author+"' , time='"+time+"', hits='"+hits+"', statues='"+statues+"'where mid='"+mid+"' ");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.close();
	}
}
